import React, { Component } from 'react';
import { Toast } from "antd-mobile"
import { httpUtil, tools } from "dcloud-utils";
import { httpUrl } from "dcloud-core";
import Router from "umi/router";

export default class Home extends React.Component {
  constructor(props) {
    super(props);
    this.state = {

    }
  }
  wxLoginFun = (openId) => {
    console.log(httpUrl.appConfig.qic);
    httpUtil.post(httpUrl.wxLogin, { endpoint: httpUrl.appConfig.endpoint, qic: httpUrl.appConfig.qic, openId: openId, t: new Date().getTime()},'form').then((response) => {
      if (response.code == 200) {
        const ticket = response.data.ticket;
        console.log('拿到ticket：', ticket)
        localStorage.setItem("ticket", ticket);
        Toast.hide();
        // 验ticket
        httpUtil.post(httpUrl.ticket, { appKey: httpUrl.appConfig.appKey, server: window.location.host, ticket: ticket, t: new Date().getTime()},'form').then((_response) => {
          // 通过ticket 换到token 会在拦截器中处理201响应:src/core/httpUtilConfig/index.js
          console.log('验证ticket后', _response)
        }).catch(error => {
          Toast.fail('系统错误', error);
        })


      } else if (response.code == 204) {
        const rst = response.data.rst
        console.log('获取到rst', rst)
        localStorage.setItem("rst", rst);
        Router.replace('/login');

        // Toast.fail(response.msg, 2);
      }
    }).catch(error => {
      Toast.fail('系统错误', 2);
    })
    Toast.hide();
  }

  getAuthOpenId () {
    // 获取到微信code回调回来
    const tenantId = localStorage.getItem("tenantId");
    const openId = sessionStorage.getItem("openId");
    if(openId){
      console.log('缓存中获取openId', openId);
      this.wxLoginFun(openId);
      return;
    }
    if (window.wxParames().code) { 
      httpUtil.post(httpUrl.getOpenId, { code: window.wxParames().code, tenantId: tenantId }).then((response) => {
        if (response.code == 200) {
          console.log('获取到了openId', response.data);
          sessionStorage.setItem("openId", response.data);
          this.wxLoginFun(response.data);
          // window.location.replace(url);
        } else {
          Toast.fail(response.msg, 2);
        }
      }).catch(error => {
        Toast.fail('系统错误', 2);
      })
    } else {
      // 第一次进页面先获取token  application/json   application/x-www-form-urlencoded
      httpUtil.get(httpUrl.getWxCodeUrl, { tenantId: tenantId }).then((response) => {
        if (response.code == 200) {
          const origin = window.location.origin;
          const pathname = window.location.pathname;
          const hash = window.location.hash;
          const reqUrl = window.location.protocol + '//' + window.location.host + '/#/login/vauth?';
          let url = reqUrl + response.msg + "&redirect_uri=" + (origin) + encodeURIComponent(pathname) + encodeURIComponent(hash) + '&isScope=' + true;
          Toast.hide();
          window.location.replace(url);
        } else {
          Toast.fail('系统异常，请稍后再试', 2);
        }
      }).catch(error => {
        Toast.fail('系统错误', 2);
      })
    }
  }

  componentDidMount() {
    const tenantId = localStorage.getItem("tenantId");
    if(tenantId){
      this.getAuthOpenId()
    }else{
      tools.getOrderConfig(()=>{
        this.getAuthOpenId()
      })
    }
  }


  render() {
    // Toast.loading('登录中~~~',0);
    return (
      <div></div>
    )
  }
}
